package com.win.oauth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.win.oauth.dto.*;
import com.win.oauth.entity.Permission;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author youlian.fei
 * @since 2020-03-14
 */
public interface PermissionMapper extends BaseMapper<Permission> {

    @Select("SELECT m.*,p.id AS permId,p.url AS url FROM  sys_module m LEFT JOIN sys_permission p ON p.type=1 AND p.module_fun_id = m.id")
    List<ModulePermissionDTO> listModule();

    @Select("SELECT f.*,p.id AS permId, p.url AS url FROM  sys_fun f LEFT JOIN sys_permission p ON p.type=2 AND p.module_fun_id = f.id")
    List<FunPermissionDTO> listFun();


    @Select({"SELECT p.id AS permId, ",
            " m.id AS moduleId, m.parent_id AS moduleParentId, m.`name` AS moduleName,",
            " f.module_id AS funModuleId, f.`name` AS funName",
            " FROM sys_permission p",
            " LEFT JOIN sys_module m ON p.type = 1 AND p.module_fun_id = m.id",
            " LEFT JOIN sys_fun f ON p.type = 2 AND p.module_fun_id = f.id",
            " ORDER BY m.seq ASC, f.seq ASC, p.id ASC"})
    List<PermModuleFunDTO> listPermModuleFun();

    @Select({"SELECT p.id AS permId, m.id AS moduleId, m.`name` AS moduleName",
            " FROM sys_permission p",
            " LEFT JOIN sys_module m ON p.module_fun_id = m.id WHERE p.type = 1 "})
    List<PermModuleDTO> listPermModule();

    @Select({"SELECT p.id AS permId, f.module_id AS funModuleId, f.`name` AS funName",
            " FROM sys_permission p",
            " LEFT JOIN sys_fun f ON p.module_fun_id = f.id WHERE p.type = 2"})
    List<PermFunDTO> listPermFun();

}
